package com.itfreer.data.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.itfreer.data.db.entity.DBRegistEntity;

public class HibernateDataUtils {

	public static Session getOpenSession(DBRegistEntity entity) {
		// 获取configue对象
		Configuration cfg = new Configuration();
		// 设置数据连接url
		cfg.setProperty(HibernateCantant.CONNECTION_URL, entity.getConnectUrl());
		// 设置数据库连接用户名
		cfg.setProperty(HibernateCantant.CONNECTION_USERNAME, entity.getUserName());
		// 设置数据库连接密码
		cfg.setProperty(HibernateCantant.CONNECTION_PASSWORD, entity.getPassWord());
		// 设置数据库连接驱动类
		cfg.setProperty(HibernateCantant.CONNECTION_DRIVER_CLASS, entity.getDriverClass());
		// 设置数据库方言
		cfg.setProperty(HibernateCantant.DIALECT, entity.getDialect());
		// 设置是否显示sql
		cfg.setProperty(HibernateCantant.SHOW_SQL, entity.getShowSql());
		// 设置是否格式化sql
		cfg.setProperty(HibernateCantant.FORMAT_SQL, entity.getFormatSql());
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		// 当前无Session时才创建，否则会绑定到当前已有线程；session在事务结束后自动关闭。
		// Session session = sessionFactory.getCurrentSession();
		Session session = sessionFactory.openSession();
		return session;
	}

	/**
	 * 获取查询sql
	 * 
	 * @param entity
	 * @param queryFields
	 * @param queryWhere
	 * @param order
	 * @return
	 */
	public static String toSelectSql(DBRegistEntity entity, String queryFields, String queryWhere, String order) {
		StringBuilder sb = new StringBuilder();

		// 获取查询表
		String tableName = getTableName(entity);

		// 设置查询字段
		if (IsNullOrSpace(queryFields)) {
			sb.append("select * from " + tableName + " ");
		} else {
			sb.append("select " + queryFields + " from " + tableName + " ");
		}
		String sqlWhere = getWhere(entity, queryWhere);
		sb.append(sqlWhere);
		// 添加排序条件
		if (!IsNullOrSpace(order)) {
			sb.append(order);
		}
		String sql = sb.toString();
		return sql;
	}

	/***
	 * 获取where 条件
	 * 
	 * @param entity
	 * @param queryWhere
	 * @return
	 */
	public static String getWhere(DBRegistEntity entity, String queryWhere) {
		StringBuilder sb = new StringBuilder();
		// 设置where查询条件
		String dfWhere = entity.getDfWhere();
		String sqlWhere = "";
		if (!IsNullOrSpace(dfWhere)) {
			sqlWhere += " and ( " + dfWhere + " ) ";
		}
		if (!IsNullOrSpace(queryWhere)) {
			sqlWhere += " and ( " + queryWhere + " ) ";
		}
		if (!IsNullOrSpace(sqlWhere)) {
			sb.append(" where 1=1 ");
		}
		sb.append(sqlWhere);
		String where = sb.toString();
		return where;
	}

	public static String toCountSql(DBRegistEntity entity, String queryWhere) {
		String tableName = getTableName(entity);
		String where = getWhere(entity, queryWhere);
		String sql = " select count(*) from " + tableName + " " + where + " ";
		return sql;
	}

	/****
	 * 获取查询的的表名
	 * 
	 * @param entity
	 * @return
	 */
	public static String getTableName(DBRegistEntity entity) {
		// 获取查询表
		String tableName = entity.getTableName();
		if (IsNullOrSpace(tableName)) {
			return "";
		}
		return tableName;
	}

	/**
	 * 判断一个字符串是否为null 或""或" "
	 */
	public static boolean IsNullOrSpace(String value) {
		if (value == null || "".equals(value)) {
			return true;
		}

		if (value.trim().length() == 0) {
			return true;
		} else {
			return false;
		}
	}

	/***
	 * 把ListList<Map<String, Object>>中的map对象的Key都处理成为大写
	 * 
	 * @param list
	 * @return
	 */
	public static List<Map<String, Object>> toUpper(List<Map<String, Object>> list) {
		List<Map<String, Object>> listTemp = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> m : list) {
			Map<String, Object> mapTemp = new HashMap<String, Object>();
			for (String k : m.keySet()) {
				mapTemp.put(k.toUpperCase(), m.get(k));
			}
			listTemp.add(mapTemp);
		}
		return listTemp;
	}
}
